﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace D3crft.Windows.UserControls {
    /// <summary>
    /// Interaktionslogik für ucButton.xaml
    /// </summary>
    public partial class ucButton : UserControl {

        public event EventHandler evClicked;

        UserControl oRoot;
        Label oLabel;
        UserControl oControl;

        bool bEnabled = true;
        bool bMouseEntered = false;
        bool bMouseDown = false;


        // ####################################################
        // #####    Class Initialization
        // ####################################################
        #region Class Initialization

        public ucButton() {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ev_Object_Loaded);
            oRoot = FindName("Root") as UserControl;
            oLabel = FindName("Label") as Label;
            oControl = FindName("Control") as UserControl;
        }

        #endregion

        public string Text {
            get { return (string)((Label)FindName("Label")).Content; }
            set { ((Label)FindName("Label")).Content = value; }
        }

        public bool Enabled {
            get { return bEnabled; }
            set { 
                bEnabled = value;
                if (IsLoaded) {
                    if (bEnabled) {
                        oRoot.Background = Application.Current.Resources.FindName("Gradient_DefaultUC") as Brush;
                        oLabel.Foreground = Application.Current.Resources.FindName("Brush_DefaultForeground") as Brush;
                        oControl.BorderBrush = Application.Current.Resources.FindName("Brush_Border") as Brush;
                    }
                    else {
                        bMouseEntered = false;
                        bMouseDown = false;
                        oRoot.Background = Application.Current.Resources.FindName("Gradient_DisabledUC") as Brush;
                        oLabel.Foreground = Application.Current.Resources.FindName("Brush_DisabledForeground") as Brush;
                        oControl.BorderBrush = Application.Current.Resources.FindName("Brush_DisabledBorder") as Brush;
                    }
                }
            }

        }

        private void OnMouseDown(object sender, MouseEventArgs ea) {
            if (!bEnabled) return;
            bMouseDown = true;
            oRoot.Background = (Brush)FindResource("Gradient_PressedUC");
            oLabel.Foreground = (Brush)FindResource("Brush_PressedForeground");
        }

        private void OnMouseUp(object sender, MouseEventArgs ea) {
            if (!bEnabled) return;
            bMouseDown = false;
            if (bMouseEntered) {
                oRoot.Background = (Brush)FindResource("Gradient_HighlightedUC");
                oControl.BorderBrush = FindResource("Brush_HighlightedBorder") as Brush;
                // Clicked
                if (evClicked != null) evClicked(this, null);
            }
            else {
                oRoot.Background = (Brush)FindResource("Gradient_DefaultUC");
                oControl.BorderBrush = FindResource("Brush_Border") as Brush;
            }
            oLabel.Foreground = (Brush)FindResource("Brush_DefaultForeground");
        }

        private void OnMouseEnter(object sender, MouseEventArgs ea) {
            if (!bEnabled) return;
            bMouseEntered = true;
            oRoot.Background = (Brush)FindResource("Gradient_HighlightedUC");
            oControl.BorderBrush = FindResource("Brush_HighlightedBorder") as Brush;
        }

        private void OnMouseLeave(object sender, MouseEventArgs ea) {
            if (!bEnabled) return;
            bMouseEntered = false;
            if (bMouseDown) {
                bMouseDown = false;
                oLabel.Foreground = (Brush)FindResource("Brush_DefaultForeground");
            }
            else {
                oControl.BorderBrush = FindResource("Brush_Border") as Brush;
            }
            oRoot.Background = (Brush)FindResource("Gradient_DefaultUC");
        }

        private void ev_Object_Loaded(object sender, EventArgs ea) {
            if (!bEnabled) {
                bMouseEntered = false;
                bMouseDown = false;
                oRoot.Background = Application.Current.FindResource("Gradient_DisabledUC") as Brush;
                oLabel.Foreground = Application.Current.FindResource("Brush_DisabledForeground") as Brush;
                oControl.BorderBrush = Application.Current.FindResource("Brush_DisabledBorder") as Brush;
            }
        }

    }
}
